# a module only for math function

# takes 2 lists seen as vector and returns 1 list which is the scalar
# product of the 2 vectors
# return an empty list on error
def vectorScalarProduct(vec1, vec2):
	if len(vec1) <> len(vec2):
		return []
	return reduce(add,[vec1[i]*vec2[i] for i in range(len(vec1))],0)

# returns the sum of 2 vectors within a list
def vectorAdd(vec1, vec2):
	if len(vec1) <> len(vec2):
		return []
	return [vec1[i] + vec2[i] for i in range(len(vec1))]


# returns the sum of 2 vectors within a list
def sumVectorElements(vect):
	return reduce(add,[vect[i] for i in range(len(vect))],0)

def add(x,y):
	return x+y

#find the root of the function func or return -999
def newton(xstart, func, epsDer, tol, maxloops ):
        x = xstart
        xold = xstart + 10 * tol#so we are sure to pass in the loop
        loops = 0
        while (abs(x - xold) > tol) and (loops < maxloops):
                loops = loops + 1
                fval = func(x)
                dfdx = 0.5 * (func(x + epsDer) - func(x - epsDer))/epsDer
                xold = x
                x = xold - fval/dfdx
        if( abs(x - xold) > tol ):
                return -999
        return x


def isARepartition( repToBeTested ):
        theSum = sumVectorElements(repToBeTested)
        errorMsg = "tyutiuytuytutyuu"
        if( abs(theSum - 100) > 0.000000001 ):
                errorMsg = "sum to %s" %theSum
                return [False,errorMsg]
        for i in range(len(repToBeTested)):
                errorMsg = "blabla"
                if( repToBeTested[i] < 0 or repToBeTested[ i ] > 100 ):
                        return [False,errorMsg]
        return [True,""]
        
        
